package com.jiayang.hhotel.presenter;

import android.app.AlertDialog;
import android.content.Context;
import android.support.annotation.IdRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.DivideAdapter;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.InfoModel;
import com.jiayang.hhotel.bean.ReservationSearchDetailsModel;
import com.jiayang.hhotel.bean.RoomNumModel;
import com.jiayang.hhotel.net.Api;
import com.jiayang.hhotel.ui.ReservationSearchDetailsActivity;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by Administrator on 2017/5/23 0023.
 * 订房查询详情页面逻辑类
 */

public class ReservationSearchDetailsPresenter extends XPresent<ReservationSearchDetailsActivity> {

    /**
     * 请求详情数据
     * @param mpLanguage 语言
     * @param mpCronum 订单号
     */
    public void asyncLoad(int mpLanguage,long mpCronum){
        Api.getHomeSer().reservationSearchDetails(Constant.PHONE,mpLanguage,1,mpCronum)
                .compose(XApi.<ReservationSearchDetailsModel>getApiTransformer())
                .compose(XApi.<ReservationSearchDetailsModel>getScheduler())
                .compose(getV().<ReservationSearchDetailsModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<ReservationSearchDetailsModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().sealPic(error.getType());
                    }

                    @Override
                    public void onNext(ReservationSearchDetailsModel Model) {
                        switch (Model.getState()){
                            case 9://请求成功
                                getV().addReservationInfo(Model.getPRsvbillRoomDetailListDtos());
                                break;
                            case -1://失败
                                getV().sealPic(-1);
                                break;
                        }
                    }
                });
    }

    /**
     * 获取房号列表
     * @param mpLanguage 语言
     * @param mpArrDate 到店日期
     * @param mpDptDate 离店日期
     * @param mpRmUseType -房间使用形式（1-房租；2-钟点；3-床租；4-月租）
     * @param mpRmType 房间等级
     */
    public void initRoomList(int mpLanguage,String mpArrDate,String mpDptDate,int mpRmUseType,String mpRmType){
        Api.getHomeSer().getRoomNumList(Constant.PHONE,mpLanguage,1,mpArrDate,mpDptDate,mpRmUseType,mpRmType)
                .compose(XApi.<RoomNumModel>getApiTransformer())
                .compose(XApi.<RoomNumModel>getScheduler())
                .compose(getV().<RoomNumModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<RoomNumModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(RoomNumModel Model) {
                        switch (Model.getState()){
                            case 9://请求成功
                                getV().initDivideDialog(Model.getPRsvforcastRoomDtoList());
                                break;
                            case -1://失败
                                ToastUtil.Toast(getV().getString(R.string.service_error),getV());
                                break;
                            case 1002://没有数据
                                ToastUtil.Toast("数据错误",getV());
                                break;
                        }
                    }
                });
    }

    /**
     * 选择房号后确定按钮
     * @param mpLanguage 语言
     * @param mpSerial 由详情页面提供
     * @param mpCRONum 订单号
     * @param mpRoom 房号
     */
    public void deterMineDivide(int mpLanguage,long mpSerial,long mpCRONum,String mpRoom){
        Api.getHomeSer().deterMineRoomNum(Constant.PHONE,mpLanguage,1,mpSerial,mpCRONum,mpRoom)
                .compose(XApi.<InfoModel>getApiTransformer())
                .compose(XApi.<InfoModel>getScheduler())
                .compose(getV().<InfoModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<InfoModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(InfoModel Model) {
                        switch (Model.getState()){
                            case 9://请求成功
                                getDivideDialog().dismiss();
                                ToastUtil.Toast(getV().getString(R.string.finish_divide),getV());
                                break;
                            case -1://失败
                                getDivideDialog().dismiss();
                                ToastUtil.Toast(getV().getString(R.string.service_error_contract_manager),getV());
                                break;
                        }
                    }
                });
    }

    AlertDialog divideDialog;//分房对话框
    /**
     * 显示分房对话框
     * @param list 此处传递对象
     * @param roomCount 可选择房数
     */
    public void createDivideDialog(final Context context, List<RoomNumModel.PRsvforcastRoomDtoListBean> list, final int roomCount){
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        View dialogView= LayoutInflater.from(context).inflate(R.layout.dialog_divide,null);
        GridView roomListView= (GridView) dialogView.findViewById(R.id.gv);
        Button deter= (Button) dialogView.findViewById(R.id.bt_deter);
        TextView title= (TextView) dialogView.findViewById(R.id.tv_title);
        ImageView back= (ImageView) dialogView.findViewById(R.id.tv_back);
        Button random = (Button) dialogView.findViewById(R.id.bt_random);//随机按钮
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDivideDialog().dismiss();
            }
        });
        title.setText(roomCount+context.getString(R.string.between));
        final DivideAdapter adapter = new DivideAdapter(context,list,roomCount);
        deter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!StringUtil.isEmpty(adapter.getRoomList())) {
                    getV().clickDeter(adapter.getRoomList());
                }else if(adapter.getSelectRoomCount()!=roomCount){
                    ToastUtil.Toast(getV().getString(R.string.insufficient_allocation_of_housing)+roomCount+context.getString(R.string.between),getV());
                }else{
                    ToastUtil.Toast(getV().getString(R.string.please_select_room_num),context);
                }
            }
        });
        random.setOnClickListener(new View.OnClickListener() {//点击随机，只会随机还未选中的最大房间数量。
            @Override
            public void onClick(View view) {
                adapter.randomRoom();
            }
        });
        roomListView.setAdapter(adapter);
        builder.setView(dialogView);
        divideDialog=builder.show();
    }

    //获取分房对话框
    public AlertDialog getDivideDialog(){
        return divideDialog;
    }

    /**
     * 保存折扣信息请求
     * @param mpLanguage 语言
     * @param mpCRONum 订单号
     * @param mpGrade 1-经理级折扣2-总监级折扣3-总经理级折扣4-免费
     */
    public void disCountSave(int mpLanguage,long mpCRONum,String mpRoom,long mpGrade){
        Api.getHomeSer().disCountBt(Constant.PHONE,mpLanguage,mpCRONum,mpRoom,mpGrade)
                .compose(XApi.<InfoModel>getApiTransformer())
                .compose(XApi.<InfoModel>getScheduler())
                .compose(getV().<InfoModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<InfoModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        isSuccess = false;
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(InfoModel Model) {
                        isSuccess = false;
                        switch (Model.getState()){
                            case 9://请求成功
                                disCountDialog.dismiss();
                                getV().getList();//打完折扣后需要刷新页面。
                                break;
                            case -1://失败
                                disCountDialog.dismiss();
                                ToastUtil.Toast(getV().getString(R.string.service_error_contract_manager),getV());
                                break;
                        }
                    }
                });
    }

    private AlertDialog disCountDialog;
    private boolean isSuccess;//点击折扣后不允许重复点击
    /**
     * 创建折扣对话框
     * @param context 上下文
     * @param tag 上次点击的是哪一项
     */
    public void createDisCountDialog(Context context,int tag){
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        View itemView=LayoutInflater.from(context).inflate(R.layout.dialog_discount,null);
        RadioGroup group  = (RadioGroup) itemView.findViewById(R.id.rg);
        RadioButton bt1 = (RadioButton) itemView.findViewById(R.id.tv_manager);
        RadioButton bt2 = (RadioButton) itemView.findViewById(R.id.tv_chief);
        RadioButton bt3 = (RadioButton) itemView.findViewById(R.id.tv_general);
        RadioButton bt4 = (RadioButton) itemView.findViewById(R.id.tv_normal);
        switch (tag){
            case 1:
                bt1.setChecked(true);
                break;
            case 2:
                bt2.setChecked(true);
                break;
            case 3:
                bt3.setChecked(true);
                break;
            case 4:
                bt4.setChecked(true);
                break;
        }
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if(!isSuccess){
                    switch (checkedId){
                        case R.id.tv_manager://经理级折扣
                            getV().saveDisCount(1);
                            break;
                        case R.id.tv_chief://总监级折扣
                            getV().saveDisCount(2);
                            break;
                        case R.id.tv_general://总经理级折扣
                            getV().saveDisCount(3);
                            break;
                        case R.id.tv_normal://免费
                            getV().saveDisCount(4);
                            break;
                    }
                    isSuccess = true;
                }
            }
        });
        builder.setView(itemView);
        disCountDialog=builder.show();
    }
}
